package ru.ifmo.filemanager.core;

import static java.lang.System.err;
import static java.lang.System.exit;
import static ru.ifmo.filemanager.core.Core.error;

import java.lang.Thread.UncaughtExceptionHandler;

class ExceptionHandler implements UncaughtExceptionHandler {

	public void uncaughtException(Thread t, Throwable e) {
		try {
			error("Fatal error in thread \"" + t.getName() + "\"", e, true);
		} catch (Throwable ee) {
			err.print("Fatal error in thread \"");
			err.print(t.getName());
			err.println("\":");
			e.printStackTrace(err);
			err.println();
			err.println("When trying to handle the error, an error occured:");
			ee.printStackTrace(err);
		}
		Exit: {
			try {
				exit(1);
			} catch (Throwable ee) {
				err.println("When trying to exit, an error occured:");
				ee.printStackTrace(err);
				break Exit;
			}
			err.println("Unable to exit. System.exit() completed normally.");
		}
		throw new Error();
	}
}